#ifndef TFFMPEGVIDEOPLAYTHREAD_H
#define TFFMPEGVIDEOPLAYTHREAD_H

#include <QThread>
#include "tffmpegsupporter.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavformat/version.h>
#include <libavutil/time.h>
#include <libavutil/mathematics.h>
#include <libavutil/ffversion.h>
#include <libavutil/imgutils.h>
#include <libswresample/swresample.h>
}

class TFFmpegVideoPlayThread : public QThread
{
    Q_OBJECT
public:
    explicit TFFmpegVideoPlayThread(QObject *parent = nullptr);
    ~TFFmpegVideoPlayThread() override;

    //开始播放
    void startPlay();
    //停止播放
    void stopPlay();
    //初始化视频
    bool initVideo();
    //设置时钟
    void setStartTime(int64_t nstarttime);
    //设置时间基准
    void setTimeBase(AVRational ntimebase);

    //解码视频
    void decodeVideo(AVPacket *npacket);
    //解码图像
    void decodeImage();

protected:
    void run() override;

private:
    volatile bool m_stopped = true;           //线程停止标志位
    qint64 m_startTime;                       //解码开始时间
    AVRational m_timeBase;                    //时间基准

    AVCodecContext *m_videoCtx = nullptr;     //视频解码Context
    AVCodec *m_videoCodec = nullptr;          //视频解码器
    SwsContext *m_videoSwsCtx = nullptr;      //视频转换

    AVFrame *m_videoFrame = nullptr;          //帧对象视频
    AVFrame *m_videoFrameDst = nullptr;       //帧对象视频解码目标

    int m_videoWidth = 0;         //视频宽度
    int m_videoHeight = 0;        //视频高度
    double m_videoFps = 0;        //视频帧率

    uint8_t *m_videoData = nullptr;   //解码后的视频数据

    TFFCustomPlayer &m_ffcustomPlayer = TFFmpegSupporter::get()->getFFCustomPlayer();

signals:
    void sigGetVideoFrame(const QImage &nvideoframe);

};

#endif // TFFMPEGVIDEOPLAYTHREAD_H
